<template>
  <lb-layout
    v-loading="$store.getters.loading"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="#a19f9fb0;"
    :paddBottom="5"
  >
    <aps-form
      :config="apsForm.formConfig"
      :model="apsForm.formData"
      :formRef="apsForm.formRef"
      :labelWidth="apsForm.labelWidth"
      ref="form"
    ></aps-form>
    <lb-table
      :column="tableData.column"
      :data="tableData.data"
      border
      :height="640"
      pagination
      background
      layout="total, sizes, prev, pager, next, jumper"
      :page-sizes="[20, 40, 60, 80]"
      :pager-count="5"
      :current-page.sync="currentPage"
      :total="total"
      :page-size="pageSize"
      @size-change="handleSizeChange"
      @p-current-change="handleCurrentChange"
    >
    </lb-table>
    <component
      :is="modelName"
      :id="modelid"
      :operateTitle="operateTitle"
      :visible.sync="show"
      @close="close"
    ></component>
  </lb-layout>
</template>

<script>
import { getWmeterlist } from "@/api/maintenance/electric.js";
import detail from "./components/operatealarmList";
export default {
  components: {
    detail,
  },
  dicts: ["device_alarm_type"],
  data() {
    return {
      //弹窗
      modelid: "",
      modelName: "",
      show: false,
      operateTitle: "",
      apsForm: {
        formRef: "form",
        labelWidth: "auto",
        formData: {},
        formConfig: {
          formAttr: {
            inline: true,
            size: "small",
          },
          properties: {
            alarmStatus: {
              label: "报警状态:",
              render: (h) => {
                let that = this;
                let arr = [];
                arr = [
                  {
                    label: "未处理",
                    value: "0",
                  },
                  {
                    label: "已处理",
                    value: "1",
                  },
                ];
                // 数据严谨处理,处理后端数据异常情况
                let valNum = that.$refs?.form?.getFieldsValue().alarmStatus;
                let a = [
                  h(
                    "el-select",
                    {
                      attrs: {
                        placeholder: "请选择报警状态",
                        value: valNum,
                        clearable: true,
                      },
                      props: ["value"],
                      on: {
                        change: (value) => {
                          that.$refs?.form?.setFieldValue("alarmStatus", value);
                          that.$forceUpdate();
                        },
                      },
                    },
                    [
                      arr.map((item) => {
                        let { label, value } = item;
                        return h("el-option", {
                          props: {
                            label,
                            value: value,
                            key: value,
                          },
                        });
                      }),
                    ]
                  ),
                ];
                return h("div", a);
              },
            },
            startAndend: {
              label: "报警时间:",
              typeName: "el-date-picker",
              type: "datetimerange",
              size: "small",
              "start-placeholder": "开始时间",
              "end-placeholder": "结束时间",
              "value-format": "yyyy-MM-dd HH:mm:ss",
            },
            alarmType: {
              label: "报警原因:",
              render: (h) => {
                let that = this;
                let arr = [];
                that.dict?.type?.device_alarm_type.some((item) => {
                  arr.push(item);
                });
                // 数据严谨处理,处理后端数据异常情况
                let valNum = that.$refs?.form?.getFieldsValue().alarmType;
                let a = [
                  h(
                    "el-select",
                    {
                      attrs: {
                        placeholder: "请选择报警原因",
                        value: valNum,
                        clearable: true,
                      },
                      props: ["value"],
                      on: {
                        change: (value) => {
                          that.$refs?.form?.setFieldValue("alarmType", value);
                          that.$forceUpdate();
                        },
                      },
                    },
                    [
                      arr.map((item) => {
                        let { label, value } = item;
                        return h("el-option", {
                          props: {
                            label,
                            value: value,
                            key: value,
                          },
                        });
                      }),
                    ]
                  ),
                ];
                return h("div", a);
              },
            },
            userName: {
              label: "处理人:",
              typeName: "el-input",
              placeholder: "请输入处理人",
            },
            phoneNum: {
              label: "联系电话:",
              typeName: "el-input",
              placeholder: "请输入联系电话",
            },
            startAndend2: {
              label: "处理时间:",
              typeName: "el-date-picker",
              type: "datetimerange",
              size: "small",
              "start-placeholder": "开始时间",
              "end-placeholder": "结束时间",
              "value-format": "yyyy-MM-dd HH:mm:ss",
            },
          },
          formSoltButton: [
            {
              label: "搜索",
              type: "primary",
              typeName: "el-button",
              size: "small",
              icon: "el-icon-search",
              click: this.handlerSearchDetail,
            },
            {
              label: "重置",
              typeName: "el-button",
              size: "small",
              icon: "el-icon-refresh-left",
              click: this.handleReset,
            },
          ],
        },
      },
      currentPage: 1,
      pageSize: 20,
      total: 0,
      tableData: {
        column: [
          {
            prop: "alarmStatus",
            label: "报警状态",
            minWidth: 120,
            align: "center",
            showOverflowTooltip: true,
            render: (h, scope) => {
              let that = this;
              let a;
              let arr = [
                {
                  label: "未处理",
                  value: "0",
                },
                {
                  label: "已处理",
                  value: "1",
                },
              ];
              let alarmStatus = scope.row.alarmStatus;
              if (alarmStatus == "0" || alarmStatus == "1") {
                arr.map((item) => {
                  if (item.value == alarmStatus) {
                    a = item.label;
                  }
                });
              } else {
                a = "未知状态";
              }
              return <div>{a}</div>;
            },
          },
          {
            prop: "alarmStartTime",
            label: "报警时间",
            width: 180,
            align: "center",
            showOverflowTooltip: true,
          },
          {
            prop: "alarmType",
            label: "报警原因",
            minWidth: 120,
            align: "center",
            showOverflowTooltip: true,
            render: (h, scope) => {
              return (
                <div>
                  <dict-tag
                    options={this.dict.type.device_alarm_type}
                    value={scope.row.alarmType}
                  />
                </div>
              );
            },
          },
          {
            prop: "userName",
            label: "处理人",
            minWidth: 100,
            align: "center",
            showOverflowTooltip: true,
          },
          {
            prop: "phoneNum",
            label: "联系电话",
            minWidth: 100,
            align: "center",
            showOverflowTooltip: true,
          },
          {
            prop: "handleTime",
            label: "处理时间",
            width: 180,
            align: "center",
            showOverflowTooltip: true,
          },
          {
            prop: "remarks",
            label: "结果描述",
            minWidth: 100,
            align: "center",
            showOverflowTooltip: true,
          },
          {
            label: "操作",
            width: 200,
            fixed: "right",
            render: (h, scope) => {
              let that = this;
              let a, b;
              a = (
                <el-button
                  type="text"
                  size="small"
                  icon="el-icon-view"
                  onClick={() => {
                    that.showModel("detail", scope.row, "报警详情");
                  }}
                >
                  详情
                </el-button>
              );
              b = (
                <el-button
                  type="text"
                  size="small"
                  icon="el-icon-check"
                  onClick={() => {
                    that.showModel("detail", scope.row, "报警处理");
                  }}
                >
                  处理
                </el-button>
              );
              return (
                <div>
                  {a}
                  {b}
                </div>
              );
            },
          },
        ],
        data: [],
      },
    };
  },
  created() {},
  mounted() {
    this.handlerSearchDetail();
  },
  methods: {
    async initData(data) {
      let that = this;
      let query = {
        ...data,
      };
      query.pageNum = that.currentPage;
      query.pageSize = that.pageSize;
      that.$store.commit("app/updateApiCount", "add");
      await getWmeterlist(query)
        .then((res) => {
          that.$store.commit("app/updateApiCount");
          if (res.code === 700) {
            this.$message.warning(res.msg);
            return false;
          }
          that.tableData.data = res?.rows ?? [];
          that.total = res?.total ?? 0;
        })
        .catch((err) => {
          that.$store.commit("app/updateApiCount");
          // this.$message.error("系统错误,请联系管理员!");
        });
    },
    // 搜索
    handlerSearchDetail() {
      const { startAndend, startAndend2, ...other } =
        this.$refs?.form?.getFieldsValue();
      const data = {
        ...other,
      };
      if (startAndend) {
        data.alarmStartTime = startAndend[0];
        data.alarmEndTime = startAndend[1];
      }
      if (startAndend2) {
        data.handleStartTime = startAndend2[0];
        data.handleEndTime = startAndend2[1];
      }
      this.initData(data);
    },
    // 重置
    handleReset() {
      this.$refs?.form?.resetFields();
      this.handlerSearchDetail();
    },
    handleSizeChange(val) {
      this.currentPage = 1;
      this.pageSize = val;
      this.handlerSearchDetail();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.handlerSearchDetail();
    },
    // 弹窗
    showModel(name, params, title) {
      this.modelName = name;
      this.modelid = params?.id || "";
      this.operateTitle = title;
      setTimeout(() => {
        this.show = true;
      }, 200);
    },
    close(status) {
      this.show = false;
      setTimeout(() => {
        if (status) this.handlerSearchDetail();
      }, 200);
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .el-form-item {
  .el-input__inner {
    width: 217px;
  }
}
</style>
